home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLSRC.PAK / TIMEGADG.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  2.7 KB  |  121 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.7  $
  6. //
  7. //----------------------------------------------------------------------------
  8. #include <owl/pch.h>
  9. #if !defined(OWL_TIMEGADG_H)
  10. # include <owl/timegadg.h>
  11. #endif
  12. #if !defined(CLASSLIB_TIME_H)
  13. # include <classlib/time.h>
  14. #endif
  15. #if !defined(CLASSLIB_POINTER_H)
  16. # include <classlib/pointer.h>
  17. #endif
  18.  
  19. OWL_DIAGINFO;
  20.  
  21. //
  22. // Constructor for TTimeGadget.
  23. //
  24. TTimeGadget::TTimeGadget(TGetTimeFunc timeFunc, int id, TBorderStyle border,
  25.                          TAlign align, uint numChars, const char* text,
  26.                          TFont* font)
  27.             :TTextGadget(id, border, align, numChars, text, font),
  28.              TimeFunc(timeFunc)
  29. {
  30.   SetShrinkWrap(false, true);
  31. }
  32.  
  33. //
  34. // Overriden from TGadget to inform gadget window to setup a timer
  35. //
  36. void
  37. TTimeGadget::Created()
  38. {
  39.   TGadget::Created();
  40.   Window->EnableTimer();
  41. }
  42.  
  43. //
  44. // Overridden from TGadget to display the current time.
  45. //
  46. bool
  47. TTimeGadget::IdleAction(long count)
  48. {
  49.   TGadget::IdleAction(count);
  50.  
  51.   string newTime;
  52.   TimeFunc(newTime);
  53.   SetText(newTime.c_str());
  54.  
  55.   // NOTE: Don't return true to drain system. Let GadgetWindow Timer
  56.   //       message indirectly trigger IdleAction.
  57.   //
  58.   return false;
  59. }
  60.  
  61. //
  62. // Retrieve the current time.
  63. //
  64. void
  65. TTimeGadget::GetTTime(string& newTime)
  66. {
  67.   TTime time;
  68.   newTime = time.AsString();
  69. }
  70.  
  71. //
  72. // Win32 specific
  73. //
  74. #if defined(BI_PLAT_WIN32)
  75.  
  76. //
  77. // Retrieve the system time using the Win32 API.
  78. //
  79. void
  80. TTimeGadget::GetSystemTime(string& newTime)
  81. {
  82.   TAPointer<char> dateBuffer = new char[100];
  83.   TAPointer<char> timeBuffer = new char[100];
  84.   LCID lcid = ::GetUserDefaultLCID();
  85.   SYSTEMTIME systemTime;
  86.   ::GetSystemTime(&systemTime);
  87.  
  88.   if (::GetTimeFormat(lcid, LOCALE_NOUSEROVERRIDE, &systemTime, 0, timeBuffer, 100)) {
  89.     if (::GetDateFormat(lcid, LOCALE_NOUSEROVERRIDE, &systemTime, 0, dateBuffer, 100)) {
  90.       newTime += dateBuffer;
  91.       newTime += " ";
  92.       newTime += timeBuffer;
  93.     }
  94.   }
  95. }
  96.  
  97.  
  98. //
  99. // Retrieve the local time using the Win32 API
  100. //
  101. void
  102. TTimeGadget::GetLocalTime(string& newTime)
  103. {
  104.   TAPointer<char> dateBuffer = new char[100];
  105.   TAPointer<char> timeBuffer = new char[100];
  106.   LCID lcid = ::GetUserDefaultLCID();
  107.   SYSTEMTIME systemTime;
  108.   ::GetLocalTime(&systemTime);
  109.  
  110.   if (::GetTimeFormat(lcid, LOCALE_NOUSEROVERRIDE, &systemTime, 0, timeBuffer, 100)) {
  111.     if (::GetDateFormat(lcid, LOCALE_NOUSEROVERRIDE, &systemTime, 0, dateBuffer, 100)) {
  112.       newTime += dateBuffer;
  113.       newTime += " ";
  114.       newTime += timeBuffer;
  115.     }
  116.   }
  117. }
  118.  
  119.  
  120. #endif
  121.